<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>解析 CSV 字符串为一个数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.str-ends-with.html">« str_ends_with</a></li>
      <li style="float: right;"><a href="function.str-ireplace.html">str_ireplace »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>解析 CSV 字符串为一个数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.str-getcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_getcsv</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">str_getcsv</span> &mdash; <span class="dc-title">
   解析 CSV 字符串为一个数组
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.str-getcsv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>str_getcsv</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$separator</code><span class="initializer"> = &quot;,&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &quot;\&quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$escape</code><span class="initializer"> = &quot;\\&quot;</span></span><br>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   以 <abbr title="Comma Separated Value">CSV</abbr> 字段格式解析字符串输入，并返回包含读取字段的数组。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    此函数考虑区域设置。如果 <code class="literal">LC_CTYPE</code> 是类似 <code class="literal">en_US.UTF-8</code>
    的值，此函数将错误的读取单字节编码的字符串。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-getcsv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       待解析的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">separator</code></dt>

     <dd>

      <p class="para">
       设定字段界定符（仅一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enclosure</code></dt>

     <dd>

      <p class="para">
       设定字段包裹字符（仅一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">escape</code></dt>

     <dd>

      <p class="para">
       设置转义字符（最多一个单字节字符）。默认为反斜线（<code class="literal">\</code>）。空字符串（<code class="literal">&quot;&quot;</code>）禁用专有转义机制。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        Usually an <code class="parameter">enclosure</code> character is escaped inside
        a field by doubling it; however, the <code class="parameter">escape</code>
        character can be used as an alternative. So for the default parameter
        values <code class="literal">&quot;&quot;</code> and <code class="literal">\&quot;</code> have the same
        meaning. Other than allowing to escape the
        <code class="parameter">enclosure</code> character the
        <code class="parameter">escape</code> character has no special meaning; it isn&#039;t
        even meant to escape itself.
       </span>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-getcsv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个包含读取到的字段的索引数组。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.str-getcsv-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.4.0</td>
       <td>
        <code class="parameter">escape</code> 现在将空字符串视为禁用专有转义机制的信号。以前视为默认参数值。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-getcsv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4823">
    <p><strong>示例 #1 <span class="function"><strong>str_getcsv()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'PHP,Java,Python,Kotlin,Swift'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_getcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(5) {
  [0]=&gt;
  string(3) &quot;PHP&quot;
  [1]=&gt;
  string(4) &quot;Java&quot;
  [2]=&gt;
  string(6) &quot;Python&quot;
  [3]=&gt;
  string(6) &quot;Kotlin&quot;
  [4]=&gt;
  string(5) &quot;Swift&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.str-getcsv-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fgetcsv.html" class="function" rel="rdfs-seeAlso">fgetcsv()</a> - 从文件指针中读入一行并解析 CSV 字段</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>